package com.cnap.multiclouldmgmt.service.impl;

import com.cnap.multiclouldmgmt.MulticlouldmgmtApplicationTests;
import com.cnap.multiclouldmgmt.model.enums.K8sStatusEnum;
import com.cnap.multiclouldmgmt.model.k8sModels.*;
import com.cnap.multiclouldmgmt.model.k8sMonitorModels.AlgReqModel;
import com.cnap.multiclouldmgmt.service.AlgInfoService;
import com.cnap.multiclouldmgmt.service.K8sClusterService;
import com.cnap.multiclouldmgmt.service.MultiCloudMgmtService;
import com.cnap.multiclouldmgmt.service.PrometheusService;
import com.cnap.multiclouldmgmt.util.CnapCloudMgmtConstants;
import org.junit.jupiter.api.Test;
import org.mockito.Mockito;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.mock.mockito.MockBean;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import static org.junit.jupiter.api.Assertions.*;

class AlgInfoServiceImplTest extends MulticlouldmgmtApplicationTests {

    @MockBean
    private MultiCloudMgmtService multiCloudMgmtService;

    @MockBean
    private K8sClusterService k8sClusterService;

    @MockBean
    private PrometheusService prometheusService;

    @Autowired
    private AlgInfoService algInfoService;

    @Test
    void getDynamicExpansionAlgInfo() throws Exception {

        //hpa -begin
        AlgReqModel algReqModel = new AlgReqModel();
        algReqModel.setK8sClusterNames(new ArrayList<>());
        algReqModel.getK8sClusterNames().add("clusterName");
        algReqModel.setSvcName("svcName");
        algReqModel.setNamespace("nameSpace");
        algReqModel.setNodeAppType("ai");

        K8sClusterBasicModel k8sClusterBasicModel = new K8sClusterBasicModel();
        k8sClusterBasicModel.setK8sClusterName("clusterName");
        k8sClusterBasicModel.setK8sStatus(
                K8sStatusEnum.Running.getChineseDescribe());
        k8sClusterBasicModel.setCloudVendor("aliyun");
        k8sClusterBasicModel.setK8sClusterIp("1.1.1.1");
        k8sClusterBasicModel.setClusterInfoJson("{\"netCost\":\"1\",\"netCostUnit\":\"元/Mbps/小时\",\"gpuCost\":\"1\",\"fileCost\":\"1\",\"gpuCostUnit\":\"元/月\",\"cpuCost\":\"1\",\"diskCost\":\"1\",\"netCostType\":\"固定带宽\",\"fileCostUnit\":\"元/GB/月\",\"diskCostUnit\":\"元/GB/月\",\"cpuCostUnit\":\"元/月\",\"fpgaCost\":\"1\"}");

        List<NodeModel> nodeModels = new ArrayList<>();
        NodeModel nodeModel = new NodeModel();
        nodeModels.add(nodeModel);
        nodeModel.setNodeName("nodeName");
        nodeModel.setNodeContainerQuota(100);
        nodeModel.setNodeCpuUseByN(10);
        nodeModel.setNodeMemQuotaByKi(100);
        nodeModel.setNodeMemUseNumByKi(10);
        nodeModel.setNodeDiskQuota(100);
        nodeModel.setNodeDiskUseNum(10);
        nodeModel.setNodeIp("2.2.2.2");
        nodeModel.setNodeLabelMap(new HashMap<>());
        nodeModel.getNodeLabelMap().put(CnapCloudMgmtConstants.NODE_APP_TYPE_LABEL+"ai","ai");

        Mockito.when(multiCloudMgmtService.getK8sClusterInfoByName(Mockito.anyString()))
                .thenReturn(k8sClusterBasicModel);
        Mockito.when(k8sClusterService.getAllNodeInfo(k8sClusterBasicModel))
                .thenReturn(nodeModels);
        Mockito.when(prometheusService.getNodeNetBandwidthBybps("1.1.1.1",
                "2.2.2.2",null)).thenReturn(2.0);
        //hpa -end

        SvcModel svcModel = new SvcModel();
        svcModel.setSvcName("svcName");
        svcModel.setSeclectorMap(new HashMap<>());
        svcModel.getSeclectorMap().put("app","svc01");

        Mockito.when( k8sClusterService.getSvcInfo(k8sClusterBasicModel,"nameSpace",
                "svcName")).thenReturn(svcModel);

        List<PodModel> podModels = new ArrayList<>();
        PodModel podModel = new PodModel();
        podModels.add(podModel);
        podModel.setPodLabelMap(new HashMap<>());
        podModel.getPodLabelMap().put("app","svc01");
        podModel.setCpuUseByN(10l);
        podModel.setPodLimitCpuByN(100l);
        podModel.setMemUseByKi(10l);
        podModel.setPodLimitMemByKi(100l);

        Mockito.when(k8sClusterService.getAllPodsInfo(k8sClusterBasicModel,"nameSpace"))
                .thenReturn(podModels);

        algInfoService.getHpaAlgInfo(algReqModel);
        algInfoService.getDynamicExpansionAlgInfo(algReqModel);

    }

    @Test
    void getCostAlgInfo() throws Exception {

        //hpa -begin
        AlgReqModel algReqModel = new AlgReqModel();
        algReqModel.setK8sClusterNames(new ArrayList<>());
        algReqModel.getK8sClusterNames().add("clusterName");
        algReqModel.setSvcName("svcName");
        algReqModel.setNamespace("nameSpace");
        algReqModel.setNodeAppType("ai");


        K8sClusterBasicModel k8sClusterBasicModel = new K8sClusterBasicModel();
        k8sClusterBasicModel.setK8sClusterName("clusterName");
        k8sClusterBasicModel.setK8sStatus(
                K8sStatusEnum.Running.getChineseDescribe());
        k8sClusterBasicModel.setCloudVendor("aliyun");
        k8sClusterBasicModel.setK8sClusterIp("1.1.1.1");
        k8sClusterBasicModel.setClusterInfoJson("{\"netCost\":\"1\",\"netCostUnit\":\"元/Mbps/小时\",\"gpuCost\":\"1\",\"fileCost\":\"1\",\"gpuCostUnit\":\"元/月\",\"cpuCost\":\"1\",\"diskCost\":\"1\",\"netCostType\":\"固定带宽\",\"fileCostUnit\":\"元/GB/月\",\"diskCostUnit\":\"元/GB/月\",\"cpuCostUnit\":\"元/月\",\"fpgaCost\":\"1\"}");

        List<NodeModel> nodeModels = new ArrayList<>();
        NodeModel nodeModel = new NodeModel();
        nodeModels.add(nodeModel);
        nodeModel.setNodeName("nodeName");
        nodeModel.setNodeContainerQuota(100);
        nodeModel.setNodeCpuUseByN(10);
        nodeModel.setNodeMemQuotaByKi(100);
        nodeModel.setNodeMemUseNumByKi(10);
        nodeModel.setNodeDiskQuota(100);
        nodeModel.setNodeDiskUseNum(10);
        nodeModel.setNodeIp("2.2.2.2");
        nodeModel.setNodeLabelMap(new HashMap<>());
        nodeModel.getNodeLabelMap().put(CnapCloudMgmtConstants.NODE_APP_TYPE_LABEL+"ai","ai");

        Mockito.when(multiCloudMgmtService.getK8sClusterInfoByName(Mockito.anyString()))
                .thenReturn(k8sClusterBasicModel);
        Mockito.when(k8sClusterService.getAllNodeInfo(k8sClusterBasicModel,null,
                CnapCloudMgmtConstants.NODE_APP_TYPE_LABEL + algReqModel.getNodeAppType().toLowerCase(),
                algReqModel.getNodeAppType().toLowerCase()))
                .thenReturn(nodeModels);
        Mockito.when(prometheusService.getNodeNetBandwidthBybps("1.1.1.1",
                "2.2.2.2",null)).thenReturn(2.0);
        //hpa -end
        algInfoService.getCostAlgInfo(algReqModel);
    }

    @Test
    void getHpaAlgInfo() {
    }

    @Test
    void getLoadBalaceDeployAlgInfo() throws Exception {

        //hpa -begin
        AlgReqModel algReqModel = new AlgReqModel();
        algReqModel.setK8sClusterNames(new ArrayList<>());
        algReqModel.getK8sClusterNames().add("clusterName");
        algReqModel.setSvcName("svcName");
        algReqModel.setNamespace("nameSpace");
        algReqModel.setNodeAppType("ai");


        K8sClusterBasicModel k8sClusterBasicModel = new K8sClusterBasicModel();
        k8sClusterBasicModel.setK8sClusterName("clusterName");
        k8sClusterBasicModel.setK8sStatus(
                K8sStatusEnum.Running.getChineseDescribe());
        k8sClusterBasicModel.setCloudVendor("aliyun");
        k8sClusterBasicModel.setK8sClusterIp("1.1.1.1");
        k8sClusterBasicModel.setClusterInfoJson("{\"netCost\":\"1\",\"netCostUnit\":\"元/Mbps/小时\",\"gpuCost\":\"1\",\"fileCost\":\"1\",\"gpuCostUnit\":\"元/月\",\"cpuCost\":\"1\",\"diskCost\":\"1\",\"netCostType\":\"固定带宽\",\"fileCostUnit\":\"元/GB/月\",\"diskCostUnit\":\"元/GB/月\",\"cpuCostUnit\":\"元/月\",\"fpgaCost\":\"1\"}");

        List<NodeModel> nodeModels = new ArrayList<>();
        NodeModel nodeModel = new NodeModel();
        nodeModels.add(nodeModel);
        nodeModel.setNodeName("nodeName");
        nodeModel.setNodeContainerQuota(100);
        nodeModel.setNodeCpuUseByN(10);
        nodeModel.setNodeMemQuotaByKi(100);
        nodeModel.setNodeMemUseNumByKi(10);
        nodeModel.setNodeDiskQuota(100);
        nodeModel.setNodeDiskUseNum(10);
        nodeModel.setNodeIp("2.2.2.2");
        nodeModel.setNodeLabelMap(new HashMap<>());
        nodeModel.getNodeLabelMap().put(CnapCloudMgmtConstants.NODE_APP_TYPE_LABEL + "ai","ai");

        Mockito.when(multiCloudMgmtService.getK8sClusterInfoByName(Mockito.anyString()))
                .thenReturn(k8sClusterBasicModel);
//        Mockito.when(k8sClusterService.getAllNodeInfo(k8sClusterBasicModel))
//                .thenReturn(nodeModels);
        Mockito.when(prometheusService.getNodeNetBandwidthBybps("1.1.1.1",
                "2.2.2.2",null)).thenReturn(2.0);
        Mockito.when(k8sClusterService.getAllNodeInfo(Mockito.any(K8sClusterBasicModel.class)
                        ,Mockito.anyString(),Mockito.anyString(),Mockito.anyString()))
                .thenReturn(nodeModels);
        //hpa -end

        SvcModel svcModel = new SvcModel();
        svcModel.setSvcName("svcName");
        svcModel.setSeclectorMap(new HashMap<>());
        svcModel.getSeclectorMap().put("app","svc01");

        Mockito.when( k8sClusterService.getSvcInfo(k8sClusterBasicModel,"nameSpace",
                "svcName")).thenReturn(svcModel);

        List<PodModel> podModels = new ArrayList<>();
        PodModel podModel = new PodModel();
        podModels.add(podModel);
        podModel.setPodLabelMap(new HashMap<>());
        podModel.getPodLabelMap().put("app","svc01");
        podModel.setCpuUseByN(10l);
        podModel.setPodLimitCpuByN(100l);
        podModel.setMemUseByKi(10l);
        podModel.setPodLimitMemByKi(100l);

        Mockito.when(k8sClusterService.getAllPodsInfo(k8sClusterBasicModel,"nameSpace"))
                .thenReturn(podModels);

        algInfoService.getLoadBalaceDeployAlgInfo(algReqModel);
    }

    @Test
    void getLoadBalanceInvokeAlgInfo() throws Exception {


        //hpa -begin
        AlgReqModel algReqModel = new AlgReqModel();
        algReqModel.setK8sClusterNames(new ArrayList<>());
        algReqModel.getK8sClusterNames().add("clusterName");
        algReqModel.setSvcName("svcName");
        algReqModel.setNamespace("nameSpace");
        algReqModel.setNodeAppType("ai");
        algReqModel.setWorkdLoadName("workdLoadName");
        algReqModel.setInitStateFlag(true);


        K8sClusterBasicModel k8sClusterBasicModel = new K8sClusterBasicModel();
        k8sClusterBasicModel.setK8sClusterName("clusterName");
        k8sClusterBasicModel.setK8sStatus(
                K8sStatusEnum.Running.getChineseDescribe());
        k8sClusterBasicModel.setCloudVendor("aliyun");
        k8sClusterBasicModel.setK8sClusterIp("1.1.1.1");
        k8sClusterBasicModel.setClusterInfoJson("{\"netCost\":\"1\",\"netCostUnit\":\"元/Mbps/小时\",\"gpuCost\":\"1\",\"fileCost\":\"1\",\"gpuCostUnit\":\"元/月\",\"cpuCost\":\"1\",\"diskCost\":\"1\",\"netCostType\":\"固定带宽\",\"fileCostUnit\":\"元/GB/月\",\"diskCostUnit\":\"元/GB/月\",\"cpuCostUnit\":\"元/月\",\"fpgaCost\":\"1\"}");

        List<NodeModel> nodeModels = new ArrayList<>();
        NodeModel nodeModel = new NodeModel();
        nodeModels.add(nodeModel);
        nodeModel.setNodeName("nodeName");
        nodeModel.setNodeContainerQuota(100);
        nodeModel.setNodeCpuUseByN(10);
        nodeModel.setNodeMemQuotaByKi(100);
        nodeModel.setNodeMemUseNumByKi(10);
        nodeModel.setNodeDiskQuota(100);
        nodeModel.setNodeDiskUseNum(10);
        nodeModel.setNodeIp("2.2.2.2");
        nodeModel.setNodeLabelMap(new HashMap<>());
        nodeModel.getNodeLabelMap().put(CnapCloudMgmtConstants.NODE_APP_TYPE_LABEL +"ai","ai");

        Mockito.when(multiCloudMgmtService.getK8sClusterInfoByName(Mockito.anyString()))
                .thenReturn(k8sClusterBasicModel);
//        Mockito.when(k8sClusterService.getAllNodeInfo(k8sClusterBasicModel))
//                .thenReturn(nodeModels);
        Mockito.when(prometheusService.getNodeNetBandwidthBybps("1.1.1.1",
                "2.2.2.2",null)).thenReturn(2.0);
        Mockito.when(k8sClusterService.getAllNodeInfo(Mockito.any(K8sClusterBasicModel.class)
                        ,Mockito.anyString(),Mockito.anyString(),Mockito.anyString()))
                .thenReturn(nodeModels);
        //hpa -end

        SvcModel svcModel = new SvcModel();
        svcModel.setSvcName("svcName");
        svcModel.setSeclectorMap(new HashMap<>());
        svcModel.getSeclectorMap().put("app","svc01");

        Mockito.when( k8sClusterService.getSvcInfo(k8sClusterBasicModel,"nameSpace",
                "svcName")).thenReturn(svcModel);

        List<PodModel> podModels = new ArrayList<>();
        PodModel podModel = new PodModel();
        podModels.add(podModel);
        podModel.setPodLabelMap(new HashMap<>());
        podModel.getPodLabelMap().put("app","svc01");
        podModel.setCpuUseByN(10l);
        podModel.setPodLimitCpuByN(100l);
        podModel.setMemUseByKi(10l);
        podModel.setPodLimitMemByKi(100l);
        podModel.setNodeName("nodeName");
        WorkLoadModel workLoadModel = new WorkLoadModel();
        podModel.setWorkLoadModel(workLoadModel);
        workLoadModel.setWorkLoadName("workdLoadName");

        Mockito.when(k8sClusterService.getAllPodsInfo(k8sClusterBasicModel,"nameSpace"))
                .thenReturn(podModels);

        algInfoService.getLoadBalanceInvokeAlgInfo(algReqModel);

    }
}